* set directory to where the csv files reside and Newman.dta should be saved to
clear

local semesters "Spring2016 Fall2016 Spring2017 Spring2018 Fall2018 Spring2019"
tempfile responses
save `responses', emptyok

foreach semester in `semesters' {
	import delimited rawdata/`semester'.csv, varnames(1) clear
	gen qs="`semester'"
	append using `responses'
	save `responses', replace
	}
	
drop if regexm(startdate,"[sS]tart") // gets rid of 2nd and 3rd title lines
drop if status=="Survey Preview" // gets rid of test runs
keep q* defaultq

label define semester 1 "Spring2016" 2 "Fall2016" 3 "Spring2017" 4 "Spring2018" 5 "Fall2018" 6 "Spring2019"
encode qs, gen(semester) label(semester)
label define prediction 0 "Respondents (Newman and Chiasson)" 1 "Petitioner (Government)"
encode q1, gen(prediction) label(prediction)
destring q2_1, gen(certainty)
label define role 1 "Petitioner" 2 "Justice" 3 "Respondent"
encode q3, gen(role) label(role)
count if q4=="No" // q4, asked only in spring 2016, asked if the student played the role assigned by email
assert r(N)==1 // one said no
encode q5, gen(role_deviation) label(role) // q5 recorded the role actually played by that student, in answer to the conditional follow-up question to response "No" to q4
label var role_deviation "role played in deviation from email assignment (spring 2016)"
destring q9, gen(group)
encode q6, gen(gender)
gen permission=q7=="Yes"
gen debias=substr(defaultq,1,3)=="Q10"
replace debias=. if mi(defaultq)

gen testcondition=semester=="Spring2016":semester
replace testcondition=3 if debias==1
recode testcondition (0=2)
label define testcondition 1 "Pre-class notice" 2 "In-class, no debiasing" 3 "In-class + debiasing"
label values testcondition testcondition

drop q* defaultq
drop if group==32 & semester=="Spring2019":semester // 2 students who weren't in a full team, one of whom had heard about case in other class before and was thus excluded
egen semester_x_group = group(semester group)
xtset semester_x_group
order semester group semester_x_group debias role prediction certainty gender permission
compress
save Newman.dta, replace
